﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReflexTérkép.Tests.dto;

namespace ReflexTérkép.Tests
{
    [TestClass]
    public class IdentifierTests : TestBase
    {
        [TestMethod]
        public void Can_Add_An_Identifier()
        {

            // Arrange
            const string identifier = "FirstName";

            // Act
            ReflexMapper.Config.AddIdentifier(typeof (Customer), identifier);

            var identifiers = ReflexMapper.Helpers.GetIdentifiers(typeof (Customer));

            // Assert
            Assert.IsTrue(identifiers.First() == identifier);
        }

        [TestMethod]
        public void Can_Add_Multiple_Identifiers()
        {
            // Arrange
            var identifierList = new List<string> {"FirstName", "LastName"};

            // Act
            ReflexMapper.Config.AddIdentifiers(typeof (Customer), identifierList);

            var identifiers = ReflexMapper.Helpers.GetIdentifiers(typeof (Customer));

            // Assert
            foreach (var identifier in identifierList)
            {
                Assert.IsTrue(identifiers.Contains(identifier));
            }
        }

        [TestMethod]
        public void Can_Use_Default_Conventions_To_Find_An_Identifier()
        {
            // Act
            var identifiers = ReflexMapper.Helpers.GetIdentifiers(typeof (Customer));

            //Assert
            Assert.IsTrue(identifiers.First() == "CustomerId");
        }

        [TestMethod]
        public void Can_Use_A_Custom_Convention_To_Find_An_Identifier()
        {
            // Act
            ReflexMapper.Config.IdentifierConventions.Add(type => type.Name + "Id");

            var identifiers = ReflexMapper.Helpers.GetIdentifiers(typeof(Person));

            //Assert
            Assert.IsTrue(identifiers.First() == "Id");
        }

        [TestMethod]
        public void Can_Find_An_Identifier_When_A_Field_Or_Property_Has_An_Id_Attribute()
        {
            // Act
            var identifiers = ReflexMapper.Helpers.GetIdentifiers(typeof(Customer));

            //Assert
            Assert.IsTrue(identifiers.First() == "CustomerId");

        }

        [TestMethod]
        public void Can_Find_Identifiers_When_Multiple_Fields_Or_Properties_Have_An_Id_Attribute()
        {
            // Act
            var identifiers = ReflexMapper.Helpers.GetIdentifiers(typeof(Customer));

            //Assert
            Assert.IsTrue(identifiers.Contains("CustomerId") && identifiers.Contains("CustomerType"));
        }
    }
}
